class BoardPostsController < ApplicationController
  before_filter :auth ,:except => ['feed']
  layout "common_layout"
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    self.get_board_by_name

#    @board_post_pages = Paginator.new self, BoardPost.count, 10, params[:page]
#    @board_posts = BoardPost.find :all, :conditions => [" board_id = ? ",@board.id] , 
#	                  :order => 'register_date DESC',
#                          :limit  =>  @board_post_pages.items_per_page,
#                          :offset =>  @board_post_pages.current.offset

    @board_post_total = BoardPost.count :conditions=> ['board_id = ? AND (fl_enable = 1 OR user_id = ? ) ', @board.id,session[:user_id]]

    @board_post_pages, @board_posts = paginate(:board_post, 
		:conditions => 
			['board_id = ? AND (fl_enable = 1 OR user_id = ? ) ', @board.id,session[:user_id]], 
                :order         => 'register_date DESC', 
                :per_page     => 20)
  end

  def show
    self.get_board_by_name

    @board_post = BoardPost.find(params[:id])
    @user = User.find_by_id(session[:user_id])
  end

  def new
    self.get_board_by_name

    @board_post = BoardPost.new
    @board_post.fl_enable = 1

  end

  def create
    self.get_board_by_name

    @board_post = BoardPost.new(params[:board_post])
    @board_post.user_id = session[:user_id]

    if @board_post.save
      flash[:notice] = 'BoardPost was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    self.get_board_by_name
    @board_post = BoardPost.find(params[:id])
  end

  def update
    self.get_board_by_name

    @board_post = BoardPost.find(params[:id])

    if(@board_post.user_id != session[:user_id])
	flash[:notice] = "not user_id"
	redirect_to :action=> "show",:id=> @board_post
    else 
	if @board_post.update_attributes(params[:board_post])
	  flash[:notice] = 'BoardPost was successfully updated.'
	  redirect_to :action => 'show', :id => @board_post
	else
          render :action => 'edit'
	end
    end
  end

  def destroy
    BoardPost.find(params[:id]).destroy
    redirect_to :action => 'list'
  end

  def get_board_by_name
    
   @board = Board.find_by_board_name(params[:board_name])

  end

  def feed 
        @board = self.get_board_by_name
        @board_posts = BoardPost.find(
                            :all,
                            :conditions => 
                                ['board_id = ? and fl_enable = 1 ',@board.id],
                            :order => 'register_date DESC',
                            :limit => 20)

        render :layout=>false
  end

end
